package com.cdxy.biz.process.impl;

import com.cdxy.biz.process.IRegsBizProcess;
import com.cdxy.dmr.dataObject.AxUserDO;
import com.cdxy.dmr.repository.AxUserRepository;
import com.cdxy.enums.BizErrorEnum;
import com.cdxy.enums.KaptchaEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.base.exception.enums.CommonErrorEnum;
import com.cdxy.model.dto.req.RegsEmailCodeReq;
import com.cdxy.model.dto.req.RegsMobileCodeReq;
import com.cdxy.model.dto.req.SendEmailCodeReq;
import com.cdxy.model.dto.req.SendMobileCodeReq;
import com.cdxy.service.IEmailService;
import com.cdxy.service.IRegsService;
import com.cdxy.service.ISmsService;
import com.cdxy.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: tangy
 * @Date: 2020/3/17 10:33
 */
@Service
public class RegsBizProcessImpl implements IRegsBizProcess {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private IEmailService emailService;

    @Autowired
    private IRegsService regsService;

    @Autowired
    private AxUserRepository axUserRepository;

    @Override
    public void regsSendSms(SendMobileCodeReq sendMobileCodeReq) {
        String phone = sendMobileCodeReq.getPhone();

        AxUserDO axUserDO = axUserRepository.getByMobile(phone);
        if (axUserDO != null) {
            throw new BusinessException(BizErrorEnum.REGS_ACCOUNT_EXIST_REPEAT);
        }
        smsService.sendSms(KaptchaEnum.BusinessCode.REGS_SMS, phone);
    }

    @Override
    public void regsByMobileCode(RegsMobileCodeReq regsMobileCodeReq) {
        String phone = regsMobileCodeReq.getPhone();
        if (!CommonUtil.checkPhone(phone)) {
            throw new BusinessException(CommonErrorEnum.PARAM_ERROR, "手机号错误");
        }
        regsService.regsByMobileCode(regsMobileCodeReq);
    }

    @Override
    public void regsSendEmail(SendEmailCodeReq sendEmailCodeReq) {
        String email = sendEmailCodeReq.getEmail();
        if (!CommonUtil.checkEmail(email)) {
            throw new BusinessException(CommonErrorEnum.PARAM_ERROR, "邮箱错误");
        }
        AxUserDO axUserDO = axUserRepository.getByEmail(email);
        if (axUserDO != null) {
            throw new BusinessException(BizErrorEnum.REGS_ACCOUNT_EXIST_REPEAT);
        }
        emailService.sendEmail(KaptchaEnum.BusinessCode.REGS_EMAIL, email);
    }

    @Override
    public void regsByEmailCode(RegsEmailCodeReq regsEmailCodeReq) {
        String email = regsEmailCodeReq.getEmail();
        if (!CommonUtil.checkEmail(email)) {
            throw new BusinessException(CommonErrorEnum.PARAM_ERROR, "邮箱错误");
        }
        regsService.regsByEmailCode(regsEmailCodeReq);
    }
}
